define [
  'views/base/view'
], () ->

  # Imports
  # =============================================================
  BaaseView = require 'views/base/view'

  # ModalView class
  # =============================================================

  class ModalView extends BaaseView

    # Override Attributtes
    # =============================================================

    autoRender : yes
    className  : 'modal fade'
    attributes:
      'tabindex'      : -1
      'role'          : 'dialog'
      'data-backdrop' : 'static'
      'data-keyboard' : 'false'

    # Additional Attributtes
    # =============================================================

    modalTitle       : 'Título'
    closeLabel       : 'Fechar'
    submitLabel      : 'Salvar'
    showFooter       : true
    showSubmitButton : true
    submitId         : 'modal-submit-button'

    template: """
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h3 id="modal-title">{{modalTitle}}</h3>
          </div>

          <div class="modal-body">{{html bodyTemplate bodyArguments}}</div>

          {{#if showFooter}}
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">{{closeLabel}}</button>
              {{#if ../showSubmitButton}}
                <a class="btn btn-primary" id="{{submitId}}">{{submitLabel}}</a>
              {{/if}}
            </div>
          {{/if}}
        </div>
      </div>
    """

    bodyTemplate: 'Body template'

    # Override Methods
    # =============================================================

    getTemplateData :->
      _.extend super,
        modalTitle       : @modalTitle
        closeLabel       : @closeLabel
        submitLabel      : @submitLabel
        submitId         : @submitId
        bodyTemplate     : @bodyTemplate
        showFooter       : @showFooter
        showSubmitButton : @showSubmitButton
        bodyArguments    : {}

    afterRender: ->
      super

      # Eventos
      # events = {}
      # events["click ##{@submitId}"] = 'clickSubmitButtonHandler'
      # @delegateEvents events

    # Additional Methods
    # =============================================================

    # Faz o cache dos elementos do DOM que são manipulados durante a execução
    cacheDOMElements: ->
      @DOMElements =
        $modalHeader  : @$el.find '.modal-header'
        $modalBody    : @$el.find '.modal-body'
        $modalFooter  : @$el.find '.modal-footer'
        $submitButton : @$el.find '#' + @submitId

    # Exibe a modal, dispara um evento 'modal:show' e remove o scrool da tela
    show: ->
      @$el.modal
        show     : true
        backdrop : 'static'
        keyboard : false

      @trigger 'modal:show'

    # Esconde a modal, dispara um evento 'modal:hide' e inclui o scrool da tela
    hide: ->
      @$el.modal 'hide'

      @trigger 'modal:hide'

      do $('.tooltip').remove

    clickSubmitButtonHandler: (event) ->
      event.preventDefault()
